//
//  WCAddrListVC.m
//  CBT
//
//  Created by apple on 2019/7/8.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "WCAddrListVC.h"
#import "WCAddrListCell.h"
#import "WCAddrEditVC.h"
#import "UserManager.h"
#import "WCAddressListModel.h"

@interface WCAddrListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contents;
@end

@implementation WCAddrListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = kLocalizedString(@"管理收货地址",nil);
    self.contents = [NSMutableArray array];
    
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 80;
    [self.tableView registerClass:[WCAddrListCell class] forCellReuseIdentifier:@"addrlistCell"];
    
    [self getAddressList];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (NSString *)handlePhone:(NSString *)number {
    if (nil == number) {
        return @"";
    }
    if (number.length < 7) {
        return number;
    }
    
    return [number stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contents.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    WCAddrListCell *cell = [WCAddrListCell createTableView:tableView];
    WCAddressListModel *model = self.contents[indexPath.row];
    
    cell.userNameLabel.text    = model.name;
    cell.phoneNumberLabel.text = [self handlePhone:model.phone];
    cell.addrDetailLabel.text  = [NSString stringWithFormat:@"%@%@%@%@",model.province,model.city,model.area,model.address];
    cell.editblock = ^{
        WCAddrEditVC *vc = [[WCAddrEditVC alloc] init];
        vc.addressId = model.addressId;
        vc.userName  = model.name;
        vc.phone     = model.phone;
        vc.address   = model.address;
        vc.province  = model.province;
        vc.city      = model.city;
        vc.area      = model.area;
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    return cell;
}

- (void)getAddressList {
    @weak(self)
    [UserManager getAddressRecordWithUserId:[CBTLocalUserManager sharedInstance].userId page:@"" block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            [self.contents removeAllObjects];
            NSArray *contents = data[@"data"];
            if (contents) {
                for (NSDictionary *sub in contents) {
                    WCAddressListModel *model = [WCAddressListModel mj_objectWithKeyValues:sub];
                    [self_weak_.contents addObject:model];
                }
            }
            [self_weak_.tableView reloadData];
        }
        else {
            [HFStatusView showFailureMessage:kLocalizedString(@"网络异常，请稍后重试", nil)];
        }
    }];
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//
//    WCAddrEditVC *vc = [[WCAddrEditVC alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
//}

- (UITableView *)tableView {
    if (!_tableView) {
        CGRect rect = self.view.frame;
        rect.size.height -= HFNavigationHeight;
        _tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
        
        _tableView.delegate     = self;
        _tableView.dataSource   = self;
        _tableView.scrollsToTop = YES;
        
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
        
        __weak WCAddrListVC *weakSelf = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.tableView.mj_footer.state = MJRefreshStateIdle;
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf getAddressList];
        }];
        
        if (@available(iOS 11.0, *)){
            self.tableView.estimatedRowHeight = 0;
            self.tableView.estimatedSectionHeaderHeight = 0;
            self.tableView.estimatedSectionFooterHeight = 0;
        }
    }
    return _tableView;
}


@end
